<?php
if( !defined('IN') ) die('bad request');
include_once( AROOT . 'controller'.DS.'app.class.php' );

class itemController extends appController
{
	function __construct()
	{
		parent::__construct();
		$this->check_login();
	}
	
	function index()
	{
		
		global $db_table_prefix;
		$data['title'] = $data['top_title'] = "物品信息";
		$data['outVar']=array();
		$data['html_data']= '';

		$data['outVar']['nav_src']=user_nav_html('item');

		$uid = uid();
		$data['outVar']['uid']=_kis_encrypt($uid);
		
		$mode=v('mode');

		$src = '';
		if(empty($mode)){
			//only index
		}else{
			$mode = intval($mode);

			if($mode<1){
				$src .= '参数无效！';
			}else{
				$ret_data =self::process_by_mode($mode);
				$src .= $ret_data['msg'];
				if(isset($ret_data['log'])){
					$src .= str_ireplace(PHP_EOL, '<br/>', $ret_data['log']) ;
				}
			}
		}		
		$data['html_data']= $src;
		return render( $data , 'web' , 'item.index' );
	}

	function show_all_link()
	{
    	global $limit, $offset;
    	global $db_table_prefix;

		$table_name = '`'.$db_table_prefix.'character_info`';
		$id = 'id' ;
		// $limit = 0;
		// $offset = 15;
		$fields_str = '`id`,`title`, `code`,`is_his`, `ud1`, `ud2`, `ud3`, `ud4`, `ud5`, `ud6`, `ud7`, `ud8`,`create_date`, `modify_date`';

		$where = ' WHERE `is_his`=?i AND `category`=?i AND `parent_id`=?i ORDER BY `title`,`modify_date` DESC ';
		$where = prepare($where ,array(0,0,0) );

		// echo 'DEBUG: ' . __FUNCTION__ . '() where=' . $where . PHP_EOL;
		// echo 'DEBUG: ' . __FUNCTION__ . '() limit=' . $limit . PHP_EOL;
		// echo 'DEBUG: ' . __FUNCTION__ . '() offset=' . $offset . PHP_EOL;
		$ret_data = get_search_result_json($table_name,$id,$where,$limit,$offset,$fields_str);

		$json = json_encode($ret_data);

		$data['html_data']= $json;
		return render( $data , 'web' , 'item.index' );
	}

	public static function process_by_mode($mode)
	{	
		global $db_table_prefix;
		$ret_data=array('err_code'=>0,'func_name'=>__FUNCTION__,'msg'=>'');	

		switch ($mode) {
			case 1:

				$item_src_tpl = file_get_contents(TPLROOT.'part'.DS.'item.add.tpl.html');

				$ret_data['msg'] .=$item_src_tpl;
				
				break;

			case 2:
// --查看各个分类下面的记录数目
// 			SELECT  `category` ,  `sub_category` ,CONCAT(`category` , '-', `sub_category`) as cc , COUNT(*) 
// FROM  `shdic_sc2015_item_info` GROUP BY cc
				
				@date_default_timezone_set("Asia/Shanghai");
				
				$tmp_src = '';
				$tmp_sql = "SELECT `category` , CONCAT(`category` , '-', `sub_category`) as cc , COUNT(*) as count_num FROM `".$db_table_prefix."item_info` GROUP BY cc ";
				// $tmp_msg .=$tmp_sql.PHP_EOL;
				if( $data =get_data($tmp_sql) ){

					foreach ($data as $key => $value) {
						if(isset($value['category'])) {
							$data[$key]['category'] = '<span class="cat1">'.$value['category'].'</span>';
						}
						if(isset($value['cc'])) {
							$data[$key]['cc'] = '<span class="cat1_2">'.$value['cc'].'</span>';
						}
					}

					$skip_item_str='';
					$show_table_head=true;
  					$delimiter=null;
  					$opt=array();
  					$opt['cn_head']=array('cc'=>'分类','category'=>'大分类','sub_category'=>'小分类','count_num'=>'记录数');
  					// $opt['link_arr']=array('cc'=>'api.php?ks=1&c=item&a=cclist&cc=[cc]|link'
  					// 	,'category'=>'api.php?ks=1&c=item&a=list&cat1=[category]|link');
					$tmp_src = kis_debug_table($data,$skip_item_str,$show_table_head, $delimiter,$opt);
				}
				$ret_data['msg'] .=$tmp_src;

				break;

			case 3:
				
				break;
			
		
			default:
				$ret_data['msg']='无效的参数！';
				$ret_data['err_code']=1;
				break;
		}
		
		return $ret_data;	
	}

	
}